package com.example.yifei.ui.page.main.workOrder

import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.example.yifei.common.paging.simplePager
import com.example.yifei.data.http.HttpService
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

/**
 * @Author: ZhuDaiHao
 * @Date: 2022/5/13
 * @Describe:
 */

@HiltViewModel
class WorkOrderViewModel @Inject constructor(private var service: HttpService) : ViewModel() {
    var list by mutableStateOf(ArrayList<WorkOrderBean>())

    init {
        list.add(WorkOrderBean())
        list.add(WorkOrderBean())
        list.add(WorkOrderBean())
    }

    /**
     * 请求网络
     */
    private val pager by lazy {
        simplePager {
            if (it >= 3) {
                throw NullPointerException("")
            }
            delay(2000)
            service.getWorkOrderData(it)
        }.cachedIn(viewModelScope)
    }

    var viewStates by mutableStateOf(WorkOrderViewState(pagingData = pager))
        private set

}


data class WorkOrderViewState(
    val isRefreshing: Boolean = false,
    val listState: LazyListState = LazyListState(),
    val pagingData: PagingWorkOrder
)

typealias PagingWorkOrder = Flow<PagingData<WorkOrderBean>>

data class WorkOrderBean(
    val code: String = "需求单号：W00000101",
    val state: String = "待上传",
    val image: String = "https://img95.699pic.com/element/40129/2738.png_860.png"
)

